import storage from "store";
import { SystemToken, UserToken, Login, Logout } from "@/services/user";
import { getTimestamp } from "@/utils/util";
import { removeAuthorization } from "@/utils/request";
import notification from "ant-design-vue/lib/notification";
const {
  VUE_APP_SYSTEM_TOKEN,
  VUE_APP_USER_TOKEN,
  VUE_APP_USER_ROUTES_KEY
} = process.env;

export default {
  namespaced: true,
  state: {
    token: "",
    name: ""
  },
  mutations: {
    SET_TOKEN: (state, payload) => {
      state.token = payload;
    }
  },

  actions: {
    // 获取系统TOKEN
    getSystemToken() {
      storage.remove(VUE_APP_SYSTEM_TOKEN);
      storage.remove(VUE_APP_USER_TOKEN);
      storage.remove('User-Token');
      storage.remove(VUE_APP_USER_ROUTES_KEY);
      return new Promise((resolve, reject) => {
        var params = getTimestamp();
        SystemToken(params)
          .then(res => {
            const response = res.data;
            if (response && response.data) {
              const result = response.data.token;
              storage.set(VUE_APP_SYSTEM_TOKEN, result, 48 * 60 * 60);
              storage.set('User-Token', result, 48 * 60 * 60);
              resolve("OK");
            } else {
              notification.error({
                message: "错误",
                description: "获取二维码失败"
              });
              reject(new Error("无效系统授权"));
            }
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    // 获取用户TOKEN
    UserToken(context, payload) {
      return new Promise((resolve, reject) => {
        if (payload) {
          UserToken(payload)
            .then(res => {
              const response = res.data;
              if (
                response &&
                response.code === "1000" &&
                response.data &&
                response.data.token
              ) {
                storage.remove(VUE_APP_SYSTEM_TOKEN);
                storage.remove('User-Token');
                storage.remove('User-Code');
                storage.remove('Organization-Id')
                storage.set('User-Name', response.data.name, 15 * 60 * 60)
                storage.set('User-Token', response.data.token, 15 * 60 * 60)
                storage.set('User-Code', response.data.code, 15 * 60 * 60)
                storage.set('Organization-Id', response.data.organization, 15 * 60 * 60)
                storage.set('Dept-Id', response.data.organization, 15 * 60 * 60)
                storage.set('User-Organization-Id', response.data.organization, 15 * 60 * 60)
                const relations = response.data.role_relation
                let role_relation = []
                if (relations != null) {
                  role_relation = relations.filter(el => el.relation_type !== 2)
                }
                storage.set('role_relation', role_relation, 15 * 60 * 60)
                resolve(response);
              } else {
                notification.error({
                  message: "错误",
                  description: "人员信息未获取,请重新扫码"
                });
                reject(new Error("人员授权无效"));
              }
            })
            .catch(error => {
              if (error?.message.indexOf("Network Error") != -1)
                error = "服务器链接超时，请稍后再试。";
              notification.error({
                message: "错误",
                description: error || "人员信息未获取"
              });
              reject(error);
            });
        } else {
          notification.error({
            message: "错误",
            description: "系统授权无效"
          });
          reject(new Error("系统授权无效"));
        }
      });
    },

    // 二维码登录
    Login() {
      const systemToken = storage.get(VUE_APP_SYSTEM_TOKEN);
      const timeStamp = getTimestamp();
      const params = { systemToken: systemToken, timeStamp: timeStamp };
      if (params) {
        return new Promise((resolve, reject) => {
          Login(params)
            .then(res => {
              const response = res.data;
              if (response && response.data) {
                resolve(response.data);
              } else {
                notification.error({
                  message: "错误",
                  description: "钉钉二维码未获取"
                });
                reject(new Error("钉钉二维码未获取"));
              }
            })
            .catch(error => {
              notification.error({
                message: "错误",
                description: error || "钉钉二维码未获取"
              });
              reject(error);
            });
        });
      }
    },
    // 登出
    Logout() {
      return new Promise(resolve => {
        Logout().finally(() => {
          removeAuthorization();
          resolve();
        });
      });
    }
  }
};
